## RScript to replicate expected durations in event history analysis and three figures in the manuscript
## Manuscript: Time for a Haircut: Political Regimes and Sovereign Debt Restructurings
## Author: Ignacio Mamone 
## Journal: International Interactions
## Date: March 4, 2020


## Install and load the following packages
library("dplyr")
library("haven")
library("coxed")


setwd("~/ ")
debtfile <- read_dta(file = "time_for_a_haircut.dta")
options(scipen = 999)

## Declare survival of restructurings
res.surv <- Surv(debtfile$totdelay, debtfile$res)

## Replication of Expected Durations for Regime Type (Figure 1 in the Text)
## Estimate Cox Model as in Model 1 (Table 1)
mcox <- coxph(res.surv ~ e_v2x_polyarchy_4C + cbi + debt_gdp + gdppclog + exports + fixedrate 
            + resourcerents + bond_restructuring + stratanew,
            data = debtfile, ties="efron")
summary(mcox)
## Simulate Expected Durations based on Cox Model estimated above,
## with two scenarios (fully autocratic, fully democratic)
## using the Non-Parametric Step Function Approach with Bootstraping in Kropko and Harden's "coxed" package
## Note: it may take between 5 and 10 minutes to run.
coxed1 <- coxed(mcox, method = "npsf", id = debtfile$cow,
                newdata = dplyr :: mutate(debtfile, e_v2x_polyarchy_4C = 0),
                newdata2 = dplyr :: mutate(debtfile, e_v2x_polyarchy_4C = 1),
                bootstrap = TRUE)
save(coxed1, file = "coxed1.RData")
load("coxed1.RData")
summary(coxed1)
## Plot Expected Durations for Full Democratic vs Full Autocratic
coxed1plot <- rbind(coxed1$mean.diff, coxed1$mean2, coxed1$mean1)
coxed1plot$description <- c("3.Difference",
                            "2.Democratic",
                            "1.Autocratic")
coxed1plot$order <- 1:3
coxed1plot$model <- factor(coxed1plot$order, labels = coxed1plot$description)
tiff("Figure1_coxed_dem.tiff", units="in", width=5, height=4, res=600)
ggplot(coxed1plot)+ geom_segment(aes(x = model, xend = model, y = lb, yend = ub))+
  geom_hline(aes(yintercept = 0), colour = "grey")+
  geom_point(aes(x = model, y = mean))+
  coord_flip()+ ylab(expression(paste(Delta, " Expected months until deal")))+
  xlab("") + theme_classic()
dev.off()


## Replication of Expected Duration for Parliamentary Systems (Figure 3 in the Text)
## Estimate Cox Model as in Model 1 (Table S.7)
coxinterpar <- coxph(res.surv ~ e_v2x_polyarchy_4C + dpi17parliam + intpolyparliam + cbi + bond_restructuring + stratanew + debt_gdp + gdppclog
                     + exports + fixedrate + resourcerents + cluster(cow),
                     data = debtfile, ties="efron")
summary(coxinterpar)
## Simulate Expected Durations based on Cox Model estimated above,
## with two scenarios (Parliamentary, Non Parliamentary)
## using the Non-Parametric Step Function Approach with Bootstraping in Kropko and Harden's "coxed" package
## Note: it may take between 5 and 10 minutes to run.
coxedinterpar <- coxed(coxinterpar, method = "npsf",
                       newdata = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                        dpi17parliam = 0,
                                        intpolyparliam = 1 * 0),
                       newdata2 = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                         dpi17parliam = 1,
                                         intpolyparliam = 1 * 1),
                       bootstrap = TRUE)
save(coxedinterpar, file = "coxedinterpar.RData")
load("coxedinterpar.RData")
summary(coxedinterpar)
## Plot Expected Durations for Parliamentary vs Non Parliamentary
coxedinterparplot <- rbind(coxedinterpar$mean2, coxedinterpar$mean1)
coxedinterparplot$description <- c( "2.Parliamentary Democracy",
                                    "1.Non-Parliamentary Democracy")
coxedinterparplot$order <- 1:2
coxedinterparplot$model <- factor(coxedinterparplot$order, labels = coxedinterparplot$description)
tiff("Figure3_coxed_parliamentary.tiff", units="in", width=5, height=4, res=600)
ggplot(coxedinterparplot)+
  geom_segment(aes(x = model, xend = model, y = lb, yend = ub))+
  geom_hline(aes(yintercept = 0), colour = "grey")+
  geom_point(aes(x = model, y = mean))+
  coord_flip()+
  ylab(expression(paste(Delta, " Expected months until deal")))+
  xlab("")+
  theme_classic()
dev.off()

## Estimation of Expected Durations for Partisanship
## Estimate Cox Model as in Model 3 (Table S.7)
coxinterleft <- coxph(res.surv ~ e_v2x_polyarchy_4C + dpi17left + intpolyleft
                      + cbi + bond_restructuring + stratanew + debt_gdp + gdppclog
                      + exports + fixedrate + resourcerents + cluster(cow),
                      data = debtfile, ties="efron")
summary(coxinterleft)
## Simulate Expected Durations based on Cox Model estimated above,
## with two scenarios (Leftist, Non Leftist)
## using the Non-Parametric Step Function Approach with Bootstraping in Kropko and Harden's "coxed" package
## Note: it may take between 5 and 10 minutes to run.
coxedinterleft <- coxed(coxinterleft, method = "npsf",
                        newdata = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                         dpi17left = 0,
                                         intpolyleft = 1*0.000),
                        newdata2 = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                          dpi17left = 1,
                                          intpolyleft = 1*1.000),
                        bootstrap = TRUE)
save(coxedinterleft, file = "coxedinterleft.RData")
load("coxedinterleft.RData")
summary(coxedinterleft)


## Replication of Expected Durations for Ratification Requirement
## Estimate Cox Model as in Model 2 (Table S.7)
coxintertreaty <- coxph(res.surv ~ e_v2x_polyarchy_4C + v2lgtreaty + intpolytreaty 
                   + bond_restructuring + stratanew + debt_gdp + gdppclog
                   + exports + fixedrate + resourcerents + cluster(cow),
                   data = debtfile, ties="efron")
summary(coxintertreaty)
## Simulate Expected Durations based on Cox Model estimated above,
## with two scenarios (Requires Ratification, No Requirement)
## using the Non-Parametric Step Function Approach with Bootstraping in Kropko and Harden's "coxed" package
## Note: it may take between 5 and 10 minutes to run.
coxedintertreaty <- coxed(coxintertreaty, method = "npsf",
                     newdata = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                      v2lgtreaty = 0,
                                      intpolytreaty = 1 * 0),
                     newdata2 = mutate(debtfile, e_v2x_polyarchy_4C = 1, 
                                       v2lgtreaty = 1,
                                       intpolytreaty = 1 * 1),
                     bootstrap = TRUE)
save(coxedintertreaty, file = "coxedintertreaty.RData")
load("coxedintertreaty.RData")
summary(coxedintertreaty)


## Replication of Expected Durations for Different Types of Autocracy (Figure 4 in the Text)
## Estimate Cox Model as in Model 7 (Table S.5)
coxautocracies <- coxph(res.surv ~ gwf_party + gwf_military + gwf_monarchy + gwf_personal
                     + cbi + bond_restructuring + stratanew + debt_gdp + gdppclog
                     + exports + fixedrate + resourcerents + cluster(cow),
                     data = debtfile, ties="efron")
summary(coxautocracies)
## Simulate Expected Durations based on Cox Model estimated above,
## with two scenarios (Military, Other)
## using the Non-Parametric Step Function Approach with Bootstraping in Kropko and Harden's "coxed" package.
## Note: it may take between 5 and 10 minutes to run.
coxedautocracies <- coxed(coxautocracies, method = "npsf",
                       newdata = mutate(debtfile, gwf_military = 1),
                       newdata2 = mutate(debtfile, gwf_military = 0),
                       bootstrap = TRUE)
save(coxedautocracies, file = "coxedautocracies.RData")
load("coxedautocracies.RData")
summary(coxedautocracies)
## Plot Expected Durations for Military vs Other
coxedautocraciesplot <- rbind(coxedautocracies$mean2, coxedautocracies$mean1)
coxedautocraciesplot$description <- c( "2.Other Autocracies",
                                       "1.Military Autocracies")
coxedautocraciesplot$order <- 1:2
coxedautocraciesplot$model <- factor(coxedautocraciesplot$order, labels = coxedautocraciesplot$description)
tiff("Figure4_coxed_autocracies.tiff", units="in", width=5, height=4, res=600)
ggplot(coxedautocraciesplot)+
  geom_segment(aes(x = model, 
                   xend = model, 
                   y = lb, 
                   yend = ub))+
  geom_hline(aes(yintercept = 0), colour = "grey")+
  geom_point(aes(x = model, y = mean))+
  coord_flip()+
  ylab(expression(paste(Delta, " Expected months until deal")))+
  xlab("")+
  theme_classic()
dev.off()
